# coding = utf-8
import os
import re

# 定义学生的类
class student(object):
    def __init__(self):
        self.name = ''
        self.ID = ''
        self.score = ''

    def __repr__(self):
        return "{},{},{}".format(self.ID, self.name, self.score)

    def __str__(self):
        return "{},{},{}".format(self.ID, self.name, self.score)


# 增加学生信息
def add(stulist: object, stu: object):
    if search_by_ID(stulist, stu.ID ) is True:
        print( '此ID已存在')
        return False
    stulist.append( stu )
    choose = input( 'Do you want to save the result?(Y/N)' )
    if choose is 'y' or choose is 'Y':
        file_object = open( './cache/information', 'a' )
        file_object.write(stu.ID )
        file_object.write( ' ' )
        file_object.write( stu.name )
        file_object.write( ' ' )
        file_object.write(stu.score)
        file_object.write("\r\n")
        return True
    else:
        stulist.remove( stu )
        print( '信息添加失败' )


# 查找菜单
def look_up_meue(stulist):
    while True:
        print( '*' * 50 )
        print( '*{}*'.format( '通过姓名查找------1'.center( 38 ) ) )
        print( '*{}*'.format( '通过ID查找--------2'.center( 38 ) ) )
        print( '*{}*'.format( '退出查找----------3'.center( 38 ) ) )
        print( '*' * 50 )

        choose = input( '请输入你的选择' )
        if choose == '1':
            while True:
                ID = input( '输入你要找的ID:' )
                p = re.compile( '^[0-9]{3}$' )  # 正则表达式   ^开始,[取值范围],{位数},$结尾,
                if p.match( ID ):
                    break
                else:
                    print( 'ID格式错误(三位纯数字)' )
            # 如果不存在...
            search_by_ID( stulist, ID )
        elif choose == '2':
            name = input( '请输入要找姓名:' )
            search_by_name( stulist, name )
        elif choose == '3':
            return
        else:
            print( '你的输入有误,请重新输入!' )


# 通过ID查找
def search_by_ID(stulist, ID):
    for i in range( len( stulist ) ):
        if stulist[i].ID == ID:
            print( u"姓名：", )
            print( stulist[i].name, )
            print( "\t", )
            print( u"学号:", )
            print( stulist[i].ID, )
            print( "\t", )
            print( u"分数:", )
            print( stulist[i].score )
            return True
        return False


# 通过'姓名'查找
def search_by_name(stulist: object, name):
    for i in range( len( stulist ) ):
        print( u'姓名:', )
        print( stulist[i].name, )
        print( "\t", )
        print( u'学号' )
        print( stulist[i].ID, )
        print( "\t", )
        print( u"分数:", )
        print( stulist[i].score )
        return True
    return False


# 读取学生信息   初始化
def init():
    print( '初始化......' )

    file_object = open( './cache/information', 'r', encoding='utf8' )
    # print(file_object.readlines())

    for line in file_object.readlines( ):
        stu = student( )
        line = line.strip( '\n' )  # strip 删除

        s = line.split( " " )  # split 分割 ,用空格分割数据
        # print( s )
        stu.ID = s[0]
        stu.name = s[1]
        stu.score = s[2]

        stulist.append(stu)
    print(stulist)
    print( '初始化成功' )
    return stulist


# 主菜单
def main_(stulist):
    while True:
        print( '*' * 50 )
        print( '-----------------------菜单-----------------------' )
        print( '*{}*'.format( '添加信息-----------1'.center( 48 ) ) )
        print( '*{}*'.format( '查找信息-----------2'.center( 48 ) ) )
        print( '*{}*'.format( '删除信息-----------3'.center( 48 ) ) )
        print( '*{}*'.format( '输出信息-----------4'.center( 48 ) ) )
        print( '*{}*'.format( '信息排序-----------5'.center( 48 ) ) )
        print( '*{}*'.format( '退出---------------0'.center( 48 ) ) )
        print( '-' * 50 )
        print( '*' * 50 )

        choose = input( '请输入你的选择:' )
        if choose == '1':
            stu = student( )
            stu.name = input( '输入学生姓名:' )
            while True:
                stu.ID = input( '输入学生ID:' )
                p = re.compile( '^[0-9]{3}$' )  # 正则表达式   ^开始,[取值范围],{位数},$结尾,
                if p.match( stu.ID ):
                    break
                else:
                    print( 'ID格式错误(三位纯数字)' )

            while True:
                stu.score = eval( input( '输入分数(0-100)' ) )
                if re.match( '^[0-9]', str( stu.score ) ) and 0 <= stu.score <= 100:
                    break
                else:
                    print( '你输入的分数错误(0-100)' )

        elif choose == '2':
            look_up_meue( stulist )  # 查找函数

        elif choose == "0":
            exit()

if __name__ == '__main__':  # 判断文件入口
    stulist = []
    stulist = init()  # 从文件中读取所有的学生信息
    # print(stulist)
    main_( stulist )  # 菜单
